package com.peng.leetcode.array;

import java.util.Arrays;

/**
 * MaxScore
 *
 * 1423. 可获得的最大点数
 *
 * @author: lupeng6
 * @create: 2021/2/7 11:15
 */
public class MaxScore {

    public static void main(String[] args) {
//        int[] cardPoints = {96,90,41,82,39,74,64,50,30};
        int[] cardPoints = {1, 2, 3, 4, 5, 6, 1};
        System.out.println(new MaxScore().maxScore(cardPoints, 3));
    }

    public int maxScore(int[] cardPoints, int k) {
        int windowSize = cardPoints.length - k;
        int totalSum = Arrays.stream(cardPoints).sum();

        int minSum = Integer.MAX_VALUE;

        int windowSum = 0;
        for (int i = 0; i < windowSize; i++) {
            windowSum += cardPoints[i];
        }

        minSum = Math.min(minSum, windowSum);

        for (int i = windowSize; i < cardPoints.length; i++) {
            windowSum += cardPoints[i];
            windowSum -= cardPoints[i - windowSize];

            minSum = Math.min(minSum, windowSum);
        }

        return totalSum - minSum;
    }
}
